package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.DingtalkUserRelationDao;
import cn.topevery.garden.entity.po.DingtalkUserRelation;
import cn.topevery.garden.entity.query.DingtalkUserRelationQuery;
import cn.topevery.garden.entity.vo.DingtalkUserRelationVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author code-generate
 * @date 2021-09-02
 */
@Repository
public class DingtalkUserRelationDaoImpl extends BaseDaoImpl<DingtalkUserRelation> implements DingtalkUserRelationDao {
    @Override
    public <P extends DbCsp> List<DingtalkUserRelation> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<DingtalkUserRelationVO> listPage(DingtalkUserRelationQuery query) {
        String table = "t_dingtalk_user_relation tdur";

        String columns = "tdur.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tdur.c_id desc");
        sqlQueryMate
                .where(true, "tdur.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, DingtalkUserRelationVO.class);
    }

    @Override
    public List<Map<String, Object>> getNotSyncUser() {
        String table = "t_sys_user tsu left join t_dingtalk_user_relation tdur on tsu.c_id=tdur.c_pmi_user_id";
        String columns = "tsu.c_id pmi_user_id,tsu.c_user_mobile mobile ";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tdur.c_id desc");
        sqlQueryMate
                .where(true, "tsu.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "IFNULL(tdur.c_user_id,'y')", ScriptConditionEnum.Eq, "y")
        ;
        return getMap(sqlQueryMate);
    }

    @Override
    public List<Map<String, Object>> getSyncUser() {
        String table = "t_dingtalk_user_relation tdur";
        String columns = "tdur.c_user_id userId";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tdur.c_id desc");
        sqlQueryMate
                .where(true, "tdur.c_db_status", ScriptConditionEnum.Eq, 0)
        ;
        return getMap(sqlQueryMate);
    }
}
